
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.catalina.Session;

import com.xyz.Disc;
import com.xyz.DiskType;
import com.xyz.Film;
import com.xyz.FilmContainer;
import com.xyz.Service;

@WebServlet(value="/AddDisc")
public class AddDisc extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
    public AddDisc() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    Service srv=new Service();
    
	protected void doPost(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
		
		FilmContainer FilmCont=LoadFilm.getFc();
		List<Film> lf=FilmCont.getFilms();
		Film reqFilm=null;
		
		//---
		//System.out.println(req.getParameter("check1"));
		//System.out.println(req.getParameter("check2"));
		//System.out.println(req.getParameter("film"));
		//---
		
		String filmId=req.getParameter("film");
		
		for (Film f: lf){
			if (f.getId().equals(filmId)){
				reqFilm=f;
			}
		}
		
		HttpSession session=req.getSession();
		List<Disc> userDiscs=new ArrayList<Disc>();
		if (session.getAttribute("discs")!=null){
			userDiscs=(List<Disc>)session.getAttribute("discs");
		}
		
		if (req.getParameter("check1")!=null && req.getParameter("check1").equals("on")){
			Disc newDisc1=new Disc(reqFilm, DiskType.DVD);
			//srv.addToBasket(newDisc1);
			userDiscs.add(newDisc1);
		}
		if (req.getParameter("check2")!=null && req.getParameter("check2").equals("on")){
			Disc newDisc2=new Disc(reqFilm, DiskType.Blue_Ray);
			//srv.addToBasket(newDisc2);
			userDiscs.add(newDisc2);
		}
		
		
		
		session.removeAttribute("discs");
		session.setAttribute("discs", userDiscs);
		//req.setAttribute("discs", srv.getBaskets().get(0).getDisks());
		req.setAttribute("discs", userDiscs);
		RequestDispatcher view = req.getRequestDispatcher("/WEB-INF/jsp/displayer.jsp");
		view.forward(req, response);
		
	}
}
